var trades = [];
var $swipeWrap;
// 获取工种信息
var getTrades = function(callback) {
	api.ajax({
		url: SERVER_URL + '/entity/config/trades',
		method: 'get',
		timeout: 30,
		dataType: 'json',
		returnAll: false,
		data: {}
	}, callback);
};
// 渲染工种信息
var renderTrades = function() {
	var colNum = 4;
	var rowHtml = '';
	var rowArray = [];
	trades.some(function(item, i) {
		if (i % colNum == 0) {
			if (rowArray.length) {
				rowHtml += '<div class="row">' + rowArray.join('') + '</div>';
			}
			rowArray = [];
		}
		rowArray.push('<div class="col" tapmode onclick="openWaiters(\'' + item.name + '\');">');
		rowArray.push('<img src="' + SERVER_URL + '/entity/file/' + item.icon + '" alt="">');
		rowArray.push('<div class="">');
		rowArray.push(item.name);
		rowArray.push('</div>');
		rowArray.push('</div>');
	});
	if (rowArray.length) {
		rowHtml += '<div class="row">' + rowArray.join('') + '</div>';
	}
	$api.append($swipeWrap, '<div class="swipe-box">' +
		rowHtml +
		'</div>');
};
// 打开服务员界面
var openWaiters = function(trades) {

	var delay = 0;

	if ("ios" != api.systemType) {
		delay = 300;
	}

	api.openWin({
		name: "waiters",
		url: "./waiters.html",
		pageParam: {
			trades: trades
		},
		bounces: false,
		vScrollBarEnabled: false,
		hScrollBarEnabled: false,
		reload: true,
		delay: delay
	});

};
// 页面加载完成
apiready = function() {

	$swipeWrap = $api.dom('.swipe-wrap');
	getTrades(function(result, err) {
		if (result) {
			trades = result.values;
			renderTrades();
			var mySwiper = Swipe($api.byId('swipe'), {
				continuous: true
			});
		} else {
			api.alert({
				msg: ('错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode)
			});
		};
	});
};
